package utilities.event;

import hibernatesrc.billStream.AccommodatingRec;
import hibernatesrc.billStream.Bill;
import hibernatesrc.billStream.ConcreteBill;
import hibernatesrc.billStream.MergedBill;
import hibernatesrc.billStream.Service;
import hibernatesrc.billStream.ServiceRec;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;

import utilities.Updatable;
import utilities.utilities_components.SumUp;

public class IDListener implements EventListener{
	private SumUp controller;
	private Updatable updateItem;
	
	public IDListener( SumUp controller, Updatable updateItem ){
		this.controller = controller;
		this.updateItem = updateItem;
	}
	
	public void onEvent( Event event ){
		Calendar calendar = controller.getDate();
		updateItem.update( getData( calendar.get( Calendar.YEAR ), calendar.get( Calendar.MONTH ) ), session );
	}
	
	/*"入账日期", "计天房金额", "钟点房金额", "房费总额", "费用金额", "话费金额", "其他费用总金额", "日收入总金额"*/
	private List getData( int year, int month, Session session){
		List ret=new LinkedList();
		
		Calendar from=Calendar.getInstance();
		
		from.set(Calendar.YEAR,year);
		from.set(Calendar.MONTH,month + 1);
		from.set(Calendar.DAY_OF_MONTH,0);
		Date toTime=from.getTime();
		
		from.set(Calendar.DAY_OF_MONTH,1);
		Date fromTime=from.getTime();
		
		List bills=getBillByTime(fromTime,toTime,Bill.getClass(),session);
		Iterator itr=bills.iterator();
		ConcreteBill temp=null;
		Service phoneService=Service.getService("话费");
		
		Stack stack=new Stack();
		while(itr.hasNext()){
			temp=(ConcreteBill)itr.next();
			if(temp.group != null)continue;
			
			List col=new LinkedList();
			
			col.add(temp.getTimePay());
			
			double dayRoomPrice=0,
				   hourRoomPrice=0,
				   totalRoomPrice=0,
				   totalServicePrice=0,
				   phonePrice=0,
				   otherServicePrice=0,
				   grandTotal=temp.getPrice();
				   
			stack.clear();
			stack.push(temp);
			while(!stack.empty()){
				Bill current=(Bill)stack.pop();
				if(current instanceof ConcreteBill){
					
					Iterator itrRoom=((ConcreteBill)current).getAccommodatingList().iterator();
					while(itrRoom.hasNext()){
						Double price=( Double )((AccommodatingRec)itrRoom.next()).getPrice().getFirst();
						if(current.getCountByHour())
							hourRoomPrice+=price;
						else dayRoomPrice+=price;
						totalRoomPrice+=price;
					}
					
					Iterator itrService=((ConcreteBill)current).getServiceList().iterator();
					while(itrService.hasNext()){
						ServiceRec next=(ServiceRec) itrService.next();
						Double price=( Double )((ServiceRec)next).getPrice().getFirst();
						if(next.getService().getInner() == phoneService)
							phonePrice+=price;
						else otherServicePrice+=price;
						totalServicePrice+=price;
					}
					
				}else{
					Iterator itr2=((MergedBill)current).getBills().iterator();
					while(itr2.hasNext())
						stack.push(itr2.next());
				}
			}
			
			col.add(dayRoomPrice);
			col.add(hourRoomPrice);
			col.add(totalRoomPrice);
			col.add(totalServicePrice);
			col.add(phonePrice);
			col.add(otherServicePrice);
			col.add(grandTotal);
			ret.add(col);
		}
		return ret;
	}
}